package com.web709.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web709.entity.MomentImage;
import com.web709.entity.UserMoment;
import com.web709.mapper.UserMomentMapper;
import com.web709.service.MomentImageService;
import com.web709.service.UserMomentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserMomentServiceImpl extends ServiceImpl<UserMomentMapper, UserMoment> implements UserMomentService {

    @Autowired
    private MomentImageService momentImageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserMoment userMoment) {
        this.save(userMoment);
        List<MomentImage> momentImages = userMoment.getImageUrls().stream().map(img -> {
            MomentImage momentImage = new MomentImage(null, img);
            return momentImage;
        }).collect(Collectors.toList());
        if (!momentImages.isEmpty()) {
            momentImages = momentImages.stream().map(momentImage -> {
                momentImage.setMomentId(userMoment.getId());
                return momentImage;
            }).collect(Collectors.toList());
            momentImageService.saveBatch(momentImages);
        }
    }

    @Override
    public Object list(Integer userId, Integer page, Integer limit) {

        QueryWrapper<UserMoment> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        queryWrapper.orderByDesc("create_time");
        List<UserMoment> userMoments = this.list(queryWrapper);
        for (UserMoment userMoment : userMoments) {
            QueryWrapper<MomentImage> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("moment_id", userMoment.getId());
            List<MomentImage> momentImages = momentImageService.list(queryWrapper1);
            userMoment.setImageUrls(momentImages.stream().map(MomentImage::getImageUrl).collect(Collectors.toList()));
        }
        return  userMoments;



    }

    @Override
    public List<UserMoment> allList(Integer page, Integer limit, String title, String startTime, String endTime) {
        // 查询所有朋友圈
        QueryWrapper<UserMoment> queryWrapper = new QueryWrapper<>();
        
        // 添加标题模糊搜索条件
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }
        
        // 添加时间范围搜索条件
        if (startTime != null && !startTime.isEmpty()) {
            queryWrapper.ge("create_time", startTime + " 00:00:00");
        }
        if (endTime != null && !endTime.isEmpty()) {
            queryWrapper.le("create_time", endTime + " 23:59:59");
        }
        
        queryWrapper.orderByDesc("create_time");
        List<UserMoment> userMoments = this.list(queryWrapper);
        for (UserMoment userMoment : userMoments) {
            QueryWrapper<MomentImage> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("moment_id", userMoment.getId());
            List<MomentImage> momentImages = momentImageService.list(queryWrapper1);
            userMoment.setImageUrls(momentImages.stream().map(MomentImage::getImageUrl).collect(Collectors.toList()));
        }
        return  userMoments;

    }

    @Override
    @Transactional // 添加事务保证原子性
    public void delete(Integer id) {
        // 1. 先删除关联的图片文件
        QueryWrapper<MomentImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("moment_id", id);  // 注意字段命名规范
        momentImageService.remove(queryWrapper);

        // 2. 最后删除朋友圈主体
        this.removeById(id);
    }

    @Override
    public void update(UserMoment userMoment) {
        // 1. 先删除关联的图片文件
        QueryWrapper<MomentImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("moment_id", userMoment.getId());
        momentImageService.remove(queryWrapper);
        this.updateById(userMoment);
        List<MomentImage> momentImages = userMoment.getImageUrls().stream().map(img -> {
            MomentImage momentImage = new MomentImage(null, img);
            return momentImage;
        }).collect(Collectors.toList());
        if (!momentImages.isEmpty()) {
            momentImages = momentImages.stream().map(momentImage -> {
                momentImage.setMomentId(userMoment.getId());
                return momentImage;
            }).collect(Collectors.toList());
            momentImageService.saveBatch(momentImages);
        }
    }

}

